package com.mmlc.meimei.lichengkongjian;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;

import com.mmlc.inter.InterActivity;
import com.mmlc.inter.ViewPagerAdapter;
import com.mmlc.utils.SPUtils;
import com.mmlc.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

public class WelcomeAndGuide extends InterActivity {

    @Override
    protected int initContentView() {
        return R.layout.activity_welcome_and_guide;
    }

    ViewPager vp_guide;
    ImageView iv_welcome;
    List<View> views = new ArrayList<>();
    int[] drawables = {R.drawable.guide0, R.drawable.guide1, R.drawable.guide2, R.drawable.guide3, R.drawable.guide4};
    DisplayMetrics metrics;
    boolean isFirst=true;

    @Override
    protected void initView() {
        vp_guide = ViewUtils.getView(activity, R.id.vp_guide);
        iv_welcome = ViewUtils.getView(activity, R.id.iv_welcome);
        metrics = getResources().getDisplayMetrics();
        isFirst = (boolean) SPUtils.get(context, "isFirst", true);

        if (isFirst) {
            iv_welcome.setVisibility(View.GONE);
            for (int i = 0; i < drawables.length; i++) {
                View view = getLayoutInflater().inflate(R.layout.activity_welcome, null);

                ImageView iv_content = ViewUtils.getView(view, R.id.iv_content);
                iv_content.setImageResource(drawables[i]);

                if (i == drawables.length - 1) {
                    ViewUtils.getView(view, R.id.view).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mHandler.sendEmptyMessage(1);
                        }
                    });
                }

                views.add(view);
            }
            vp_guide.setAdapter(new ViewPagerAdapter<>(views));
        } else {
            mHandler.sendEmptyMessageDelayed(1, 2000);
        }
    }

    Handler mHandler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            SPUtils.put(context, "isFirst", false);
            switch (msg.what) {
                case 0:/** to guide */
                    startActivity(new Intent(activity, GuideActivity.class));
                    break;
                case 1:/** to main */
                    startActivity(new Intent(activity, MainActivity.class));
                    break;
                case 2:/** to login */

                    break;
            }
            finish();
        }
    };
}
